﻿using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 切换标题文本块“是否显示树型连线”。
        /// </summary>
        public static void SwitchTreeLine()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.BaseText == false)
            {
                MessageBox.Show("请先选中一个标题文本块（只能是１－４级标题之一）！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Expressions.BaseText curBt = curEx as Expressions.BaseText;

            if (curBt.ParentPanel == null)
            {
                MessageBox.Show("发生意外，未找到当前文本块的父面板！此操作无意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curBt.IsHeaderText == false)
            {
                MessageBox.Show("此文本块不是１－４级标题文本块中的某一级！此操作无意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curBt.Indent > 3)
            {
                MessageBox.Show("此文本块虽然是标题文本块，但左缩进值过大！此操作无意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curBt.ParentPanel.Direction == Orientation.Horizontal)
            {
                MessageBox.Show("此文本块的父面板是水平对齐的！此操作无意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curBt.IsCollapsed)
            {
                MessageBox.Show("此标题文本块已被折叠。此操作不会立即看出效果。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                if (curBt.GetSubIndentExpressions().Count <= 0)
                {
                    MessageBox.Show("此标题文本块无“下级”公式。此操作不会立即看出效果。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }

            ModifingItem mi = new ModifingItem();
            Action action = new Action(curBt.ExID, -1, curBt.GetType().Name,
                "ShowTreeLine", curBt.ShowTreeLine.ToString(), (!curBt.ShowTreeLine).ToString());

            // 此属性本身会修改编辑器的IsModified属性。
            curBt.ShowTreeLine = !curBt.ShowTreeLine;

            mi.Add(action);
            curBt.Editor.Do(mi);
        }

        /// <summary>
        /// 使当前子面板中所有标题文本块的“树型线”都开启。
        /// 注意：此命令执行后添加的新标题文本块不受此命令影响。
        /// </summary>
        public static void ShowAllTreeLineOfASubPanel()
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;

            Expressions.SubPanel sp = Globals.selectedEditor.SelectedExpression as Expressions.SubPanel;
            if (sp == null)
            {
                if (Globals.selectedEditor.SelectedExpression.ParentPanel != null)
                {
                    sp = Globals.selectedEditor.SelectedExpression.ParentPanel;
                }
            }

            if (sp == null)//仍找不到。
            {
                MessageBox.Show("请选中一个子面板再执行此命令！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (sp.Direction == Orientation.Horizontal)
            {
                MessageBox.Show("这个子面板是水平对齐的，绘制树型线操作无意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ModifingItem mi = new ModifingItem();
            foreach (UIElement ue in sp.BasePanel.Children)
            {
                Expressions.BaseText bt = ue as Expressions.BaseText;
                if (bt == null) continue;

                if (bt.ShowTreeLine == false)
                {
                    Action actTreeLine = new Action(bt.ExID, -1, bt.GetType().Name,
                        "ShowTreeLine", false.ToString(), true.ToString());
                    bt.ShowTreeLine = true;
                    mi.Add(actTreeLine);
                }
            }

            if (mi.ModifiedPropertyAndValues.Count > 0)
            {
                MessageBox.Show("开启了当前子面板中已存在的标题文本块的树型线功能！\r\n\r\n" +
                    "注意：除非再次执行此命令，否则在此命令执行之后添加的新标题文本块不受此命令影响。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
                sp.Editor.IsModified = true;
                sp.Editor.Do(mi);
            }
            else
            {
                MessageBox.Show("当前子面板中没有可以开启树型线的标题文本块！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public static void HideAllTreeLineOfASubPanel()
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;

            Expressions.SubPanel sp = Globals.selectedEditor.SelectedExpression as Expressions.SubPanel;
            if (sp == null)
            {
                if (Globals.selectedEditor.SelectedExpression.ParentPanel != null)
                {
                    sp = Globals.selectedEditor.SelectedExpression.ParentPanel;
                }
            }

            if (sp == null)//仍找不到。
            {
                MessageBox.Show("请选中一个子面板再执行此命令！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (sp.Direction == Orientation.Horizontal)
            {
                MessageBox.Show("这个子面板是水平对齐的，绘制树型线操作无意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ModifingItem mi = new ModifingItem();
            foreach (UIElement ue in sp.BasePanel.Children)
            {
                Expressions.BaseText bt = ue as Expressions.BaseText;
                if (bt == null) continue;

                if (bt.ShowTreeLine == true)
                {
                    Action actTreeLine = new Action(bt.ExID, -1, bt.GetType().Name,
                        "ShowTreeLine", true.ToString(), false.ToString());
                    bt.ShowTreeLine = false;
                    mi.Add(actTreeLine);
                }
            }

            if (mi.ModifiedPropertyAndValues.Count > 0)
            {
                MessageBox.Show("关闭了当前子面板中已存在的标题文本块的树型线功能！\r\n\r\n" +
                    "注意：除非再次执行此命令，否则在此命令执行之后添加的新标题文本块不受此命令影响。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
                sp.Editor.IsModified = true;
                sp.Editor.Do(mi);
            }
            else
            {
                MessageBox.Show("当前子面板中没有需要关闭树型线的标题文本块！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
    }
}
